﻿using System;
using System.Threading;
using DotNetFramework.CAP.Core.Ioc;
using DotNetFramework.CAP;
using Microsoft.Extensions.DependencyInjection;
using DotNetFramework.CAP.Core.Logger;
using System.Collections.Generic;
//using DotNetFramework.CAP.Kafka;
using DotnetFramework.CAP.Ioc;
using DotnetFramework.CAP.Nlog;

namespace Sample.ConsoleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //var configBuilder = new ConfigurationBuilder()
            //    .AddJsonFile("appsettings.json");
            //var config = configBuilder.Build();

            var Services = new DotnetFramework.CAP.Ioc.ServiceCollection();
            Services.AddCap(x =>
            {
                x.UseInMemoryStorage();
                //x.UseKafka($"172.8.10.197:6667");
                x.UseMqtt(o =>
                {
                    o.Server = "127.0.0.1";
                    o.Port = 1883;
                    o.UserName = "admin";
                    o.Password = "public";
                });
                x.DefaultGroup = "cap.queue.capweb251.v1";
            }, services =>
            {
                //services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
                services.TryAddSingleton<ILogger, Logger>();
                services.TryAddSingleton<ILoggerFactory, LoggerFactory>();
                services.AddSingleton<DotNetFramework.CAP.Core.Ioc.IServiceProvider, DotnetFramework.CAP.Ioc.ServiceProvider>();
            }, services =>
            {
                //services.AddSingleton<DotNetFramework.CAP.NodeDiscovery.IDiscoveryProviderFactory, DotnetFramework.CAP.Consul.DiscoveryProviderFactory>();
            });

            //Services.AddSingleton<EventSubscriber>();
            Services.AddScopedMuti<ICapSubscribe, EventSubscriber>();
            Services.AddScopedMuti<ICapSubscribe, EventSubscriber2>();
            //Services.AddScopedMuti<ICapSubscribe, OpService>();
            //Services.AddScopedMuti<ICapSubscribe, Op2Service>();
            //Services.AddScopedMuti<ICapSubscribe, Op2MysqlService>();

            Services.BeginRegister();

            //var pvd= Services.ServiceProvider.GetService<DotNetFramework.CAP.Core.Ioc.IServiceProvider>();
            Services.ServiceProvider.GetService<IBootstrapper>().BootstrapAsync(new CancellationToken());


            //var container = new ServiceCollection();

            //container.AddLogging(logging =>
            //{
            //    //logging.AddConfiguration(config.GetSection("Logging"));
            //    logging.AddConsole();
            //}); ;
            //container.AddCap(x =>
            //{
            //    //console app does not support dashboard

            //    //x.UseMySql("<ConnectionString>");
            //    //x.UseRabbitMQ(z =>
            //    //{
            //    //    z.HostName = "192.168.3.57";
            //    //    z.UserName = "user";
            //    //    z.Password = "wJ0p5gSs17";
            //    //});
            //    //x.UseMySql("Server=172.8.10.148;Database=test;Uid=root;Pwd=123456;");
            //    x.UseInMemoryStorage();
            //    x.UseKafka($"172.8.10.142:6667");
            //});

            //container.AddSingleton<EventSubscriber>();

            //var sp = container.BuildServiceProvider();

            //sp.GetService<IBootstrapper>().BootstrapAsync();
            //var pub = sp.GetService<ICapPublisher>();
            //int i = 0;
            //while (true)
            //{
            //    var data = new EventSubscriber.OffDate() { Date = DateTime.Now, Offset = ++i };
            //    pub.Publish("sample.console.showtime", data);
            //    Thread.Sleep(1000);
            //}
            Console.ReadLine();
        }
    }
}